package com.easy.app.controller.monitor;

import com.easy.framework.common.model.R;
import com.easy.module.system.service.ISysMonitorService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务器监控
 *
 * @author Easy
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/monitor/server")
public class ServerController {

    final ISysMonitorService sysMonitorService;

    final RedisTemplate<String, String> redisTemplate;

    @PreAuthorize("@perm.hasPermission('monitor:server:list')")
    @GetMapping()
    public R getInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("cpu", sysMonitorService.getCpuInfo());
        map.put("mem", sysMonitorService.getMemoryInfo());
        map.put("jvm", sysMonitorService.getJvmInfo());
        map.put("sys", sysMonitorService.getOsInfo());
        map.put("disk", sysMonitorService.getDiskInfo());
        Map<String, Object> redis = new HashMap<>(3);
        redis.put("info", redisTemplate.execute((RedisCallback<Object>) connection -> connection.info()));
        redis.put("dbSize", redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize()));
        map.put("redis", redis);
        return R.success(map);
    }

}
